﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using VCommons.Encryptor;
using ConfigCache;
using VCommons;
namespace Standard
{

    /// <summary>
    /// 客户端信息帮助类
    /// </summary>
    public static class ClientHelper
    {
        #region 属性信息
        /// <summary>
        /// 密文 
        /// </summary>
        public static string PasswordKey { get { return ConfigFactory.Instance.GetConfig<StandardConfig>().PasswordKey; } }

        /// <summary>
        /// 网站主Cookies主名称
        /// </summary>
        public static string SiteCookiesMainName
        {
            get { return ConfigFactory.Instance.GetConfig<StandardConfig>().SiteCookiesMainName; }
        }
        internal static string SearchRecordName = "SearchRecord";
        internal static string RentalCarName = "RentalCar";
        /// <summary>
        /// 网站主域名
        /// </summary>
        public static string CookiesDomain
        {
            get
            {
                if ("localhost".Equals(System.Web.HttpContext.Current.Request.Url.Host, StringComparison.CurrentCultureIgnoreCase))
                {
                    return "localhost";
                }
                else if (System.Web.HttpContext.Current.Request.Url.Host.EndsWith(ConfigFactory.Instance.GetConfig<StandardConfig>().DoMain, StringComparison.CurrentCultureIgnoreCase))
                {
                    return ConfigFactory.Instance.GetConfig<StandardConfig>().DoMain;
                }
                else if (System.Web.HttpContext.Current.Request.Url.Host.EndsWith(ConfigFactory.Instance.GetConfig<StandardConfig>().DoMainSecond, StringComparison.CurrentCultureIgnoreCase))
                {
                    return ConfigFactory.Instance.GetConfig<StandardConfig>().DoMainSecond;
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// 当前客户端持信息上下文
        /// </summary>
        public static Current Current
        {
            get
            {
                return new Current();
            }
        }

        #endregion

        #region 持久化信息
        #region 用户相关

        /// <summary>
        /// 永久化用户信息
        /// </summary>
        /// <param name="cui">客户端用户信息</param>
        public static void WriteUserInfo(ISimpleUserInfo cui)
        {
            WriteUserInfo(cui, false);
        }

        /// <summary>
        /// 持久化用户信息
        /// </summary>
        /// <param name="cui">客户端用户信息</param>
        /// <param name="isSave">是否就需要永久化 true：永久化</param>
        public static void WriteUserInfo(ISimpleUserInfo cui, bool isSave)
        {
            UserInfoImplement.Write(UserInfoKeys.UserID, Utility.EncryptString(cui.UserID, Utility.EncryptorType.DES), isSave);
            UserInfoImplement.Write(UserInfoKeys.Name, System.Web.HttpUtility.UrlEncode(cui.Name), isSave);
            UserInfoImplement.Write(UserInfoKeys.Email, cui.Email, isSave);
        }

        /// <summary>
        /// 用户推出登录
        /// </summary>
        static public void ClearUserInfo()
        {
            ClearUserInfo(true);
        }

        /// <summary>
        /// 用户推出登录
        /// </summary>
        static public void ClearUserInfo(bool IsNewSession)
        {
            UserInfoImplement.Write(UserInfoKeys.UserID, string.Empty);
            UserInfoImplement.Write(UserInfoKeys.Name, string.Empty);
            UserInfoImplement.Write(UserInfoKeys.Email, string.Empty);
            VCommons.Http.CookieHelper.Write(SiteCookiesMainName, string.Empty);
            VCommons.Http.CookieHelper.Remove(SiteCookiesMainName);

            if (IsNewSession)
            {
                System.Web.HttpContext.Current.Response.Cookies.Add(new System.Web.HttpCookie("ASP.NET_SessionId", string.Empty));
            }
            System.Web.HttpContext.Current.Session.Clear();
            System.Web.HttpContext.Current.Session.Abandon();
        }

        #endregion

        #region 搜索相关

        /// <summary>
        /// 添加到搜索记录
        /// </summary>
        /// <param name="keyword"></param>
        public static void AddSearchRecord(string keyword, int module)
        {
            VCommons.Http.CookieHelper.Write(SearchRecordName, keyword, module.ToString(), 525600);
        }
        /// <summary>
        /// 移除全部搜索记录
        /// </summary>
        public static void StandRemoveAllSearchRecord()
        {
            VCommons.Http.CookieHelper.Remove(SearchRecordName);
        }

        /// <summary>
        /// 移除指定搜索记录
        /// </summary>
        /// <param name="ProductID">产品ID</param>
        public static void StandRemoveAllSearchRecordByKey(string keyword)
        {
            VCommons.Http.CookieHelper.Remove(SearchRecordName, keyword);
        }

        #endregion

        #region 订车相关
        public static void AddRentalCar(RentalCar entity)
        {
            VCommons.Http.CookieHelper.WriteWithDomain(RentalCarName, VCommons.Encryptor.Utility.EncryptString(entity.ToJson(), Utility.EncryptorType.DES), 525600, CookiesDomain);
        }
        #endregion

        #endregion
    }
}